package com.android.gardensnake;


/**
 * I wrote some comments, hope you don't mind.
 * */
import java.util.Random;

import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class GetCake extends Activity {
	TextView timeDisplay, resultMessage;
	Button start;
	ImageView cake;
	RatingBar score;
	long startTime, endTime;
	Random xCoordinate = new Random();
	Random yCoordinate = new Random();
	int count = 0;
	String message[] = { "Incredible! How did you do that!?", "Outstanding!",
			"Commendable", "That was okay", "Way too slow", "Maybe i should've added a tutorial..." };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.getcake);
		timeDisplay = (TextView) findViewById(R.id.tvTimeDisplay);
		resultMessage = (TextView) findViewById(R.id.tvResultMessage);
		start = (Button) findViewById(R.id.bStart);
		cake = (ImageView) findViewById(R.id.ivCake);
		score = (RatingBar) findViewById(R.id.rbScore);
		cake.setVisibility(View.GONE);
		score.setVisibility(View.GONE);
		resultMessage.setVisibility(View.GONE);
		final RatingBar score = (RatingBar)findViewById(R.id.rbScore);
		score.setOnTouchListener(new OnTouchListener() {
		        public boolean onTouch(View v, MotionEvent event) {
		            return true;
				}
		    });

		start.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				startTime = System.currentTimeMillis();
				cake.setVisibility(View.VISIBLE);
				start.setVisibility(View.GONE);
				score.setVisibility(View.GONE);
				resultMessage.setVisibility(View.GONE);
				timeDisplay.setVisibility(View.GONE);
			}
		});

		cake.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				if (count == 10) {  
					assessScore();

				} else {
					cake.setX(xCoordinate.nextInt(600));  //do not use absolute values here
					cake.setY(yCoordinate.nextInt(900)); // and here. At Y get the height of the screen, because, in this case, if the screen's height is larger than 899, there is a possibility that the cake won't be seen by the user.
					
					count++;
				}
			}
		});

	}

	public void assessScore() {
		double totalTime;
		cake.setVisibility(View.GONE);
		endTime = System.currentTimeMillis();
		totalTime = (endTime - startTime) / (double) 1000;
		timeDisplay.setVisibility(View.VISIBLE);
		timeDisplay.setText("Total time: " + totalTime);
		start.setVisibility(View.VISIBLE);
		count = 0;  
		score.setVisibility(View.VISIBLE);
		score.setRating((float) (10 - totalTime));
		resultMessage.setVisibility(View.VISIBLE);
		if (totalTime < 10.5)
			resultMessage.setText(message[(int) Math.round(totalTime - 5)]);
		else
			resultMessage.setText("Horrific. please stop playing");
	}

}
